package br.com.gbvbahia.mybooks.adapters;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import br.com.gbvbahia.mybooks.R;
import br.com.gbvbahia.mybooks.modelos.PrateleiraVO;

public class PrateleiraAdapter extends BaseAdapter {

	private LayoutInflater inflater;
	private List<PrateleiraVO> list;
	
	public PrateleiraAdapter(Activity context, List<PrateleiraVO> list) {
		super();
		this.list = new ArrayList<PrateleiraVO>();
		for(PrateleiraVO vo : list){
			if(!TextUtils.isEmpty(vo.getNome())){
				this.list.add(vo);
			}
		}
		this.inflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}
	
	public PrateleiraAdapter(Fragment fragment, List<PrateleiraVO> list) {
		this(fragment.getActivity(), list);
	}
	

	@Override
	public int getCount() {
		return list.size();
	}

	@Override
	public Object getItem(int position) {
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View view, ViewGroup parent) {
		ViewHolder holder = null;
		if (view == null) {
			holder = new ViewHolder();
			int layoutLinha = R.layout.prat_item;
			view = inflater.inflate(layoutLinha, null);
			view.setTag(holder);
			holder.tNome = (TextView) view.findViewById(R.id.tNome);
		} else {
			holder = (ViewHolder) view.getTag();
		}
		PrateleiraVO vo = list.get(position);
		holder.tNome.setText(vo.getNome());
		return view;
	}

	// Design Patter "ViewHolder" para Android
	static class ViewHolder {
		TextView tNome;
	}
}
